import { Algorithm } from "../types"

export const dynamicGraphGeneration: Algorithm = {
  name: "动态异构图展示算法",
  description: "支持10万级别多层动态异构网络的展示",
  configOptions: [
    {
      id: "csvFile",
      label: "上传CSV数据文件",
      type: "file",
      accept: ".csv",
    },
  ],
  execute: async (config) => {
    await new Promise((resolve) => setTimeout(resolve, 2000))

    const steps = ["解析CSV数据文件...", "提取节点和边信息...", "构建图结构...", "计算图属性...", "准备可视化数据..."]

    // 模拟从CSV解析的数据
    const nodeTypes = ["人物", "组织", "地点", "事件", "物品"] as const
    type NodeType = typeof nodeTypes[number]
    const edgeTypes = ["关联", "拥有", "参与", "位于", "使用"]
    const nodeColors: Record<NodeType, string> = {
      人物: "#4285F4", // 蓝色
      组织: "#EA4335", // 红色
      地点: "#34A853", // 绿色
      事件: "#FBBC05", // 黄色
      物品: "#9C27B0", // 紫色
    }

    // 生成节点
    const nodes = Array.from({ length: 100 }, (_, i) => {
      const type = nodeTypes[Math.floor(Math.random() * nodeTypes.length)]
      return {
        id: `node-${i}`,
        label: `节点 ${i}`,
        type: type,
        color: nodeColors[type],
        size: Math.random() * 10 + 5,
        timestamp: new Date(2023, 0, 1 + Math.floor(Math.random() * 365)).getTime(),
      }
    })

    // 生成边
    const edges = Array.from({ length: 200 }, (_, i) => {
      const source = Math.floor(Math.random() * 100)
      let target = Math.floor(Math.random() * 100)
      while (target === source) {
        target = Math.floor(Math.random() * 100)
      }
      return {
        id: `edge-${i}`,
        source: `node-${source}`,
        target: `node-${target}`,
        label: edgeTypes[Math.floor(Math.random() * edgeTypes.length)],
        weight: Math.random() * 5 + 1,
        timestamp: new Date(2023, 0, 1 + Math.floor(Math.random() * 365)).getTime(),
      }
    })

    // 为Cosmograph准备数据
    const cosmographData = {
      nodes: nodes.map((node) => ({
        id: node.id,
        label: node.label,
        type: node.type,
        color: node.color,
        size: node.size,
        timestamp: node.timestamp,
      })),
      links: edges.map((edge) => ({
        id: edge.id,
        source: edge.source,
        target: edge.target,
        label: edge.label,
        weight: edge.weight,
        timestamp: edge.timestamp,
      })),
    }

    // 计算一些基本统计信息
    const nodeTypeStats: Record<string, number> = {}
    nodes.forEach((node) => {
      nodeTypeStats[node.type] = (nodeTypeStats[node.type] || 0) + 1
    })

    const edgeTypeStats: Record<string, number> = {}
    edges.forEach((edge) => {
      edgeTypeStats[edge.label] = (edgeTypeStats[edge.label] || 0) + 1
    })

    return {
      result: {
        summary: `解析完成！
节点数量: ${nodes.length}
边数量: ${edges.length}
节点类型分布: ${Object.entries(nodeTypeStats)
          .map(([type, count]) => `${type}(${count})`)
          .join(", ")}
边类型分布: ${Object.entries(edgeTypeStats)
          .map(([type, count]) => `${type}(${count})`)
          .join(", ")}`,
        graphData: {
          nodes,
          edges,
          cosmographData,
        },
      },
      steps,
      executionTime: `${(Math.random() * 500 + 1000).toFixed(2)}ms`,
    }
  },
} 